/* project:			package:		class:			credits:					date:
 * GuildMaster		skin			ButtonStateSkin	by Dolraith & Kirenno		Aug 2011
 * -- contains information needed to skin a single button state (normal, hover, down) and related methods
 * 
 * === PUBLIC FNs ===================================================================
 * 	public ButtonStateSkin (String buttonStateName)	-- constructor
 *  public Color getFontColor () {				-- returns font color
 * 	public String getImgPartPath (int part) 	-- returns the path to the part specified
 * 	public void parseInput (LinkedList <String> openTags, String curTag, String content) -- loads XML values into the skin
 */


package skin;

import java.awt.Color;
import java.util.LinkedList;

import tools.StringTools;

public class ButtonStateSkin {
	
	// === GLOBAL CLASS VARIABLES =================================================================
	private String stateName;		// normalState, rolledState, pressedState - as per skin tags
	private int buttonImagesPerState;
	private Color fontColor;
	private Color forceTransparencyColor;
	private String[] imagePathes;

	// === CONSTRUCTOR, INITIALIZERS & resets =====================================================
	public ButtonStateSkin (String buttonStateName) {		// constructor
		stateName = buttonStateName;
		fontColor = Color.GRAY;
		forceTransparencyColor = null;
		setButtonImagesPerState(1);
		resetImagePathes ();
	}
	private void resetImagePathes () {		// initializes/resets imagePathes
		imagePathes =  new String [buttonImagesPerState];
		for (int i=0; i<imagePathes.length; i++)
			imagePathes[i] = "";
	}
	
	// === GETTERS ================================================================================
	public Color getFontColor () {			 		// returns font color
		return fontColor;
	}
	public Color getForceTransparencyColor () {		// returns color to force transparency to
		return forceTransparencyColor;
	}
	public String getImgPartPath (int part) { 		// returns the path to the part specified
		if (part < imagePathes.length) {
			return imagePathes[part];
		}
		else {
			Skin.reportSkinError("skin.button.state", "part index out of bounds while reading skin object (part index = " + part + ")");
			return "";
		}
	}
	
	// === SETTERS ================================================================================
	public void setButtonImagesPerState (int value) {	// sets buttonImagesPerState to value
		buttonImagesPerState = value;
	}
	
	// === IO METHODS & parsers ===================================================================
	
	public void parseInput (LinkedList <String> openTags, String curTag, String content) {	// XML input parser
		if (content.isEmpty()) return;							// skip empty entries		
		@SuppressWarnings("unchecked")							// copy the tag structure (so removing things wont hurt the original)
		LinkedList <String> cloneOpenTags = (LinkedList<String>) openTags.clone();
		while (!cloneOpenTags.isEmpty()) {						// go through the list from outter edge to inner,
																//			sending pieces to where they belong
			if (cloneOpenTags.getFirst().equals(stateName)) 
				cloneOpenTags.removeFirst();						// remove "button" tag
			if (cloneOpenTags.isEmpty()) {							// if no further tags are present, send to this module
				processXMLTag (cloneOpenTags, curTag, content);				
				return;
			}			
			
			// handle known tags			
//			if (cloneOpenTags.getFirst().equals("some_known_tag")) {
//			} else {					// skip unknown tags
				Skin.reportSkinError("skin.button object", "XML tag not recognized (tag = " + cloneOpenTags.getFirst() + "); dropping tag");
				cloneOpenTags.removeFirst();
//			}
		}
																// when the tag structure isn't workable, give error
		String error = "->";
		for (int i=0; i<openTags.size(); i++)
			error = error.concat("->").concat(openTags.get(i));
		error = error.substring(2);
		Skin.reportSkinError("skin.button.state object", "XML structure not recognized (" + error + ")");
	}
	private void processXMLTag (LinkedList <String> openTags, String curTag, String content) {// process XML tag that belong in this class
		if (curTag.equals("imagesPerState") &&  openTags.isEmpty()) {
			int value = StringTools.convertStrToInt(content);
			if (value < 0) {
				Skin.reportSkinError("skin.button.state object", "invalid tag value for imagesPerState, its supposed to be an integer (value = "+content+"); imagesPerState value reset to 1");
				value = 1;
			}
			setButtonImagesPerState (value);
			return;	
		} else if (curTag.equals("forceTransparencyColor") && openTags.isEmpty()) {
			forceTransparencyColor = Color.decode(content);
			return;
		} else if (curTag.equals("imgBase") && openTags.isEmpty()) {
			imagePathes[0] = content;
			return;
		} else if (curTag.equals("fontColor") && openTags.isEmpty()) {
			fontColor = Color.decode(content);
			return;
		}
					// if tag is not recognized 
		System.out.println("ERROR in ButtonStateSkin.processXMLTag: There is no handler for this XML tag (tag = " + curTag + ")");
	}

//	public void parseInput (String[] parsedCfg) {				// non-XML input parser
//		// parsedCfg: 0 will always have the config name, everything after are vals
//		if (parsedCfg[0].isEmpty()) return;
//		if (parsedCfg.length < 2) return;				
//		setButtonImagesPerState(parsedCfg.length-1);
//		resetImagePathes ();	
//		for (int i=0; i < buttonImagesPerState; i++) { 			// for all images of this button state
//			if (i+1 >= parsedCfg.length) {						// if no data exists for this index, quit
//				Skin.reportSkinError (parsedCfg[0], "image path not found at index ".concat(StringTools.convertIntToStr(i)));
//				break;
//			}
//			imagePathes[i] = parsedCfg[i+1];		// otherwise load image pathes into global variables
//		}														// (add one in config index to allow for 0 = config name)
//	}

}
